import 'package:flutter/material.dart';

class ExpansionPanelItem {
  final String? headerText;
  final String? body;
  bool? isExpanded;

  ExpansionPanelItem({
    this.headerText,
    this.body,
    this.isExpanded,
  });

}

class ExpansionPanelDemo extends StatefulWidget {
  const ExpansionPanelDemo({super.key});

  @override
  State<ExpansionPanelDemo> createState() => _ExpansionPanelDemoState();
}

class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> {
  bool _isExpanded = false;
  late List<ExpansionPanelItem> _expansionPanelItems;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    _expansionPanelItems = [
      ExpansionPanelItem(
        headerText: 'Panel A',
        body: 'Content for Pannel A',
        isExpanded: false,
      ),
      ExpansionPanelItem(
        headerText: 'Panel B',
        body: 'Content for Pannel B',
        isExpanded: false,
      )
    ];

  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('ExpansionPanelDemo'),
        elevation: 0.0,
      ),
      body: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              ExpansionPanelList(
                expansionCallback:  (panelIndex, isExpanded) {
                  setState(() {
                    _expansionPanelItems[panelIndex].isExpanded = !_expansionPanelItems[panelIndex].isExpanded!;
                    
                  });
                },
                children: _expansionPanelItems.map(
                  (ExpansionPanelItem item) {
                    
                  return ExpansionPanel(
                      isExpanded: item.isExpanded!,
                      
                      headerBuilder: (BuildContext context, bool isExpanded) {
                        return Container(
                          padding: EdgeInsets.all(16.0),
                          child: Text(
                            item.headerText??'',
                            style: Theme.of(context).textTheme.titleMedium,
                          ),
                        );
                      },
                      body: Container(
                        padding: EdgeInsets.all(16.0),
                        width: double.infinity,
                        child: Text(item.body??"", style: Theme.of(context).textTheme.titleMedium,),
                      ),
                  );
                
            }).toList(),
              ),
            ],
          )),
    );
  }
}
